package com.service.database.utils;

import cn.hutool.core.util.StrUtil;

import java.sql.Types;

/**
 * 常用工具封装
 * @author xianggj
 * @Date 2021/6/4 23:09
 **/
public class CommonUtils {

    /**
     * 前端传递的非法字符
     */
    public static final String UNDEFINED = "undefined";

    /**
     * 判断非空字符
     * @param str
     * @return
     */
    public static boolean isNotBlank(String str) {
        if (StrUtil.isNotBlank(str) && !UNDEFINED.equalsIgnoreCase(str)){
            return true;
        } else {
            return false;
        }
    }

    /**
     * 字符串截取
     *  忽略括号中的内容
     *
     * @param sql 数据sql
     * @return
     */
    public static String substringNotBracketsString(String sql) {
        return getParenthesisContent(sql,false, true);
    }

    /**
     * 获取字符串括号内或者外的内容
     *  说明：不包含外层括号符号
     * @param columnstr 源字符串
     * @param inOrOut true：表示获取括号内的内容 false：表示获取括号外的内容
     * @param isself true 包含括号 false：不包含括号
     * @return
     */
    public static String getParenthesisContent(String columnstr, boolean inOrOut, boolean isself) {
        char[] chars = columnstr.toCharArray();
        int filterTier = 0; //控制变量 记录括号层数
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i< chars.length ; i++ ){
            if (chars[i] == 40){
                filterTier++;
                if (filterTier == 1){
                    if (isself){
                        sb.append(chars[i]);
                    }
                    continue;
                }
            }
            if (chars[i] == 41){
                filterTier--;
                if (filterTier == 0){
                    if (isself){
                        sb.append(chars[i]);
                    }
                    continue;
                }
            }
            // filterTier大于0 表示是括号内的内容 filterTier==0 表示括号外的内容
            if ((filterTier > 0) == inOrOut){
                sb.append(chars[i]);
            }
        }
        return sb.toString();
    }

    /**
     * 字符串类型转Int
     */
    public static int typeStrToInt(String typeInfo) {
        typeInfo = StrUtil.isBlank(typeInfo) ? "string" : typeInfo.toLowerCase();
        switch (typeInfo) {
            case "int":
            case "integer":
            case "int2":
            case "int4":
            case "smallint":
            case "tinyint":
                return Types.INTEGER;
            case "int8":
            case "bigint":
                return Types.BIGINT;
            case "float":
            case "float2":
            case "float4":
                return Types.FLOAT;
            case "float8":
            case "double":
                return Types.DOUBLE;
            case "time":
                return Types.TIME;
            case "date":
                return Types.DATE;
            case "datetime":
            case "timestamp":
            case "timestamp(6)":
                return Types.TIMESTAMP;
            case "boolean":
            case "bool":
                return Types.BOOLEAN;
            case "byte":
                return Types.BINARY;
            case "short":
                return Types.SMALLINT;
            case "character":
            case "char":
                return Types.CHAR;
            case "decimal":
            case "bigdecimal":
                return Types.DECIMAL;
            default:
                return Types.VARCHAR;
        }
    }


}
